
var http = require('http');
var work = require('./lib/timetrack');
var mySql = require('MySQL');

var db = mySql.createConnection({
	host: '116.122.36.252',
	user: 'jadesoft',
	password: '1212',
	database: 'jadesoft'
});

var server = http.createServer(function(req, res) {
	switch (req.method) {
	case 'POST':
		switch(req.url) {
		case '/': 
			work.add(db, req, res);
			break;
		case '/archive':
			work.archive(db, req, res);
			break;
		case '/delete':
			work.delete(db, req, res);
			break;
		}
		break;
	case 'GET':
		switch(req.url) {
		case '/':
			console.log('hi');
			work.show(db, res);
			break;
		case '/archived':
			work.showArchived(db, res);
		}
		break;
	}
});

db.query(
		"CREATE TABLE IF NOT EXISTS work ("
		+ "id INT(10) NOT NULL AUTO_INCREMENT, "
		+ "hours DECIMAL(5,2) DEFAULT 0, "
		+ "date DATE, "
		+ "archived INT(1) DEFAULT 0, "
		+ "description LONGTEXT, "
		+ "PRIMARY KEY(id) )",
		function (err) {
			if(err) { throw err; }
			console.log("Server Started...");
			server.listen(3000, '127.0.0.1');
		}
	);